<?php
#### FUNCTIONS #################################################################
function template() { return library('template'); }

#### TEMPLATE LIBRARY ##########################################################
class TemplateLibrary {
	
	private $variables;
	private $buffer;
	private $cachePath = 'cache';
	private $caching = true;

	#### ASSIGN ################################################################
	public function assign($key, $value = null) { 
		if (is_array($key)) foreach ($key as $key => $value) {
			$this->variables[$key] = $value;
		}
		else $this->variables[$key] = $value;	
		return $this;
	}
	
	#### GET ASSIGNED ##########################################################
	public function getAssigned() { return $this->variables; }
	
	#### SET CACHING ###########################################################
	public function setCaching($boolean) { $this->caching = $boolean; }

	#### RENDER ################################################################
	public function render($template) {

		// Grab contents via output buffering
		ob_start();
		view($template, $this->variables);
		$buffer = ob_get_contents();
		ob_end_clean();

		// Clean data
		$this->variables = null;

		return $buffer;
	}
	
	#### DRAW ##################################################################
	public function draw($template, $seconds = null, $name = null) { 
		echo $this->cache($template, $seconds, $name);
	}
	
	#### HALT ##################################################################
	public function halt($template, $seconds = null, $name = null) { 
		exit($this->cache($template, $seconds, $name));
	}
	
	#### BUFFER ################################################################
	public function buffer($template, $seconds = null, $name = null) {
		$this->buffer .= $this->cache($template, $seconds, $name);
	}
	
	#### GET BUFFER ############################################################
	public function getBuffer() { return $this->buffer; }

	#### DRAW BUFFER ###########################################################
	public function drawBuffer() { echo $this->buffer; $this->buffer = null; }
	
	#### CACHE #################################################################
	public function cache($template, $seconds = null, $name = null) {
		
		// If second parameter was not supplied do nothing
		if ($seconds === null || !is_numeric($seconds) || !$this->caching) 
			return $this->render($template);
		
		// Setup the file path
		$file = $this->cachePath.'/'.
		str_replace('/', '-', ($name ? $name : $template).'.cache');
		
		// Return existing cache file
		if (file_exists($file) 
		&& ((filemtime($file) > (time() - $seconds)) || $seconds == false)) {
			ob_start();
			include($file);
			$buffer = ob_get_contents();
			ob_end_clean();
			return $buffer;
		}
		
		// Create new cache file
		if ($seconds == false || is_int($seconds)) {
			if ($fp = fopen($file, 'w')) {
				$output = $this->render($template);
				fputs($fp, $output);
				fclose($fp);
				return $output;
			} else die('Could not write to cache file: '.$file);
		}
	}
	
	#### DELETE CACHE ##########################################################
	public function deleteCache($name) {
		// Setup the file path
		$file = $this->cachePath.'/'.
		str_replace('/', '-', $name.'.cache');
		
		if (file_exists($file)) return unlink($file);
		else return false;
	}
}
?>